﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WqLunTan.Areas.Manage.Models;
using WqLunTan.Filters;
using WqLunTan.Models.DataBase;

namespace WqLunTan.Areas.Manage.Controllers
{
    /// <summary>
    /// 提供给管理员登录的控制器
    /// </summary>
    [Auth(AuthType = WqUserAuth.Admin)]
    public class AccountController : BaseManageController
    {
        /// <summary>
        /// 查看正常用户列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IActionResult List(string search, int pageIndex = 1, int pageSize = 20)
        {
            List<WqUser> list;
            var query = DbContext.Users.Where(x => x.Enable);
            var page = new PageModel() { CurrentPageIndex = pageIndex };

            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(x => x.NickName.Contains(search) || x.UserName.Contains(search));
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.IsAdmin).OrderByDescending(x => x.UserAuth).OrderByDescending(x => x.LoginTime).ThenBy(x => x.CreateTime).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }
            else
            {
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.IsAdmin).OrderByDescending(x => x.UserAuth).OrderByDescending(x => x.LoginTime).ThenBy(x => x.CreateTime).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }

            ViewData["enable"] = true;
            ViewData["enable-count"] = DbContext.Users.Where(x => x.Enable).Count();
            ViewData["disable-count"] = DbContext.Users.Where(x => x.Enable == false).Count();
            ViewData["page"] = page;

            return View(list);
        }

        /// <summary>
        /// 查看正常用户列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IActionResult DisableList(string search, int pageIndex = 1, int pageSize = 20)
        {
            List<WqUser> list;
            var query = DbContext.Users.Where(x => x.Enable == false);
            var page = new PageModel() { CurrentPageIndex = pageIndex };

            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(x => x.NickName.Contains(search) || x.UserName.Contains(search));
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.IsAdmin).OrderByDescending(x => x.UserAuth).OrderByDescending(x => x.LoginTime).ThenBy(x => x.CreateTime).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }
            else
            {
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.IsAdmin).OrderByDescending(x => x.UserAuth).OrderByDescending(x => x.LoginTime).ThenBy(x => x.CreateTime).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }

            ViewData["enable"] = false;
            ViewData["enable-count"] = DbContext.Users.Where(x => x.Enable).Count();
            ViewData["disable-count"] = DbContext.Users.Where(x => x.Enable == false).Count();
            ViewData["page"] = page;

            return View("List", list);
        }

        /// <summary>
        /// 用户详情页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Detail(Guid id)
        {
            var user = DbContext.Users.Where(x => x.Id == id).SingleOrDefault() ?? new WqUser();
            return View(user);
        }

        /// <summary>
        /// 修改用户权限等数据, 超级管理员不能被禁止登录
        /// </summary>
        /// <param name="user"></param>
        /// <param name="UserAuth"></param>
        /// <returns></returns>
        public IActionResult UpdateUser(WqUser user, List<WqUserAuth> UserAuth)
        {
            var model = DbContext.Users.Where(x => x.Id == user.Id).SingleOrDefault();
            if (model != null)
            {
                if (model.IsAdmin)
                {
                    model.PhoneNumber = user.PhoneNumber ?? string.Empty;
                    model.Email = user.Email ?? string.Empty;
                    model.LastUpdate = DateTimeOffset.UtcNow;
                }
                else
                {
                    model.PhoneNumber = user.PhoneNumber ?? string.Empty;
                    model.Email = user.Email ?? string.Empty;
                    model.LastUpdate = DateTimeOffset.UtcNow;
                    model.Enable = user.Enable;
                }
                if (UserAuth != null && UserAuth.Count > 0)
                {
                    var auth = UserAuth.FirstOrDefault();
                    foreach (var item in UserAuth)
                    {
                        auth |= item;
                    }
                    model.UserAuth = auth;  // 超级管理员设置了也不起作用
                }

                DbContext.SaveChanges();
            }
            return JsonOkResult("保存成功");
        }
    }
}